package com.k.demo.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.k.demo.entity.CustomerUser;
import com.k.demo.entity.Food;
import com.k.demo.service.FoodService;


@CrossOrigin
@RestController
@RequestMapping("/food")
public class FoodController {

	@Autowired
	private FoodService foodservice;
	@Value("${lc.upload.path}")
	private String uploadPath;
	
	/**
	 * 返回全部菜品
	 * url /food/all
	 * @author zzg
	 */
	@RequestMapping("/all")
	public Object all() {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("foodList", foodservice.getAll());
		
		return map;
	}
	
	/**
	 * 返回全部菜品 按分页展示
	 * url /food/all/page
	 * @author zzg
	 */
	@RequestMapping("/all/page")
	public Object allpage(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("foodList", foodservice.getpage((int)map.get("pageNum"),(int)map.get("pageSize")));
		
		return rmap;
	}
	
	/**
	 * 根据分类搜索菜品
	 * url /food/searchfoodbykind
	 * @author zzg
	 */
	@RequestMapping("/searchfoodbykind")
	public Object searchfoodbykind(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("foodList", foodservice.searchFoodByKind((int)map.get("foodkindid")));
		
		return rmap;
	}
	
	/**
	 * 根据分类搜索菜品 按分页展示
	 * url /food/searchfoodbykind/page
	 * @author zzg
	 */
	@RequestMapping("/searchfoodbykind/page")
	public Object getpagebykind(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("foodList", foodservice.getpagebykind((int)map.get("pageNum"),(int)map.get("pageSize"),(int)map.get("foodkindid")));
		
		return rmap;
	}
	
	/**
	 * 根据名称搜索菜品 模糊查询
	 * url /food/searchfoodbyname
	 * @author zzg
	 */
	@RequestMapping("/searchfoodbynameandkind")
	public Object searchfoodbynameandkind(@RequestBody Food food) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		//System.out.println(map.get("foodname"));
		
		rmap.put("foodList", foodservice.searchFoodByNameAndKind(food));
		
		return rmap;
	}
	
	/**
	 * 根据名称搜索菜品 模糊查询 按分页展示
	 * url /food/searchfoodbyname/page
	 * @author zzg
	 */
	@RequestMapping("/searchfoodbynameandkind/page")
	public Object getpagebynameandkind(@RequestBody Map<String,Object> map) {
		
		
		Food food = new Food((String)map.get("food_name"),(Integer)map.get("food_kind_id"));
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("foodList", foodservice.getpagebynameandkind((Integer)map.get("pageNum"),(Integer)map.get("pageSize"),food));
		
		return rmap;
	}

	
	/**
	 * 显示菜品详情
	 * url /food/detail
	 * @author zzg
	 */
	@RequestMapping("/detail")
	public Object detail(@RequestBody Map<String,Object> map) {
		return foodservice.detail((int)map.get("foodid"));
	}
	
	
	//后台功能 增删改查，查直接共用前台的功能就可以了
	@RequestMapping("/upload")
	public Object upload(@RequestParam MultipartFile[] mpfs,HttpServletRequest request) {
		
		
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		String saveFileName = UUID.randomUUID().toString().replaceAll("-", "") + "_" + mpfs[0].getOriginalFilename();
		String filePath = uploadPath+ "\\" + saveFileName;
		System.out.println(filePath);

		File f = new File(filePath);
		
			
		try {
			mpfs[0].transferTo(f);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(f.exists());
		
		map.put("picName",saveFileName);
		
		return map;
	}
	
	@RequestMapping("/addfood")
	public Object addFood(@RequestBody Food food) {
		
		Map<String,Object> map = new HashMap<String, Object>();
						
		map.put("insertnum", foodservice.addFood(food));
		
		return map;
	}
	
	@RequestMapping("/updatefood")//更新这个传的food对象需要带foodid,即一个完整的food对象，同时，前端请求时要先上传图片，上传后返回路径给temp的路径赋值再传过来
	public Object updateFood(@RequestBody Food food){
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		int count = foodservice.updateFood(food);
		
		map.put("updatenum", count);
		return map;
	}
	
	@RequestMapping("/deletefood")
	public Object delete(@RequestBody String deletelist) {
		
		JSONObject jsonObject = JSON.parseObject(deletelist);
		
		List<Integer> delete_list = JSON.parseArray(jsonObject.getString("delete_list"), Integer.class);
		
		int count = foodservice.deleteForEach(delete_list);
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("deletenum", count);
		
		return map;
	}
	
	/**
	 * 通过传入的字符串返回结果
	 * url /food/showOrderBy
	 * @author zzg
	 */
	@RequestMapping("/showOrderBy")
	public Object showOrder(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("foodListByOrder", foodservice.getAllByOrderWithPage((Integer)map.get("pageNum"), (Integer)map.get("pageSize"), (String)map.get("orderBy")));
		
		return rmap;
		
		
	}
	
	/**
	 * 通过传入的食物对象进行组合查询
	 * url /food/searchByFood
	 * @author shy
	 */
	@RequestMapping("/searchByFood")
	public Object showOrder(@RequestBody Food food,@RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize,
							@RequestParam(value = "lowestPrice") Double lowestPrice,@RequestParam(value = "highestPrice") Double highestPrice,@RequestParam(value = "orderBy") String orderBy) {
		System.out.println(food);
		Map<String,Object> rmap = new HashMap<String, Object>();
	
		rmap.put("searchList",foodservice.getFoodByFoodWithPage(pageNum, pageSize, food,lowestPrice,highestPrice,orderBy));
		
		return rmap;
		
		
	}
	
}
